import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';
import Confirm from 'vux/src/components/confirm/index.vue'


export default {
  name: 'mine',
  components: {Confirm},
  data () {
    return {
      height: '',
      openId: '',
      siteId: '',
      studentId: '',
      wxAvatar: '',
      pageNo: 1,
      total: '0',
      gradeNameShow: true,
      studentName: '',
      personMessageShow: true,
      myClassShow: true,
      myNoticeShow: true,
      myEwmShow: false,
      qjTeacherShow: false,
      t_show: false,
      content: [],
      show: false,
      tishi: '确定退出登录?',
      iflogined: false,
      appId: '',
      scanQR: false
    }
  },
  mounted () {
    var _this = this
    _this.height = document.body.clientHeight + 'px'
    _this.studentId = window.sessionStorage.getItem('studentId')
    this.appId = sessionStorage.getItem("appId")
    if (_this.studentId == '-1') {
      _this.tishi = '您目前尚未登录，是否进行登录？'
      _this.iflogined = false
      _this.show = true
    } else {
      _this.iflogined = true
      _this.openId = window.sessionStorage.getItem('openId')
      _this.siteId = window.sessionStorage.getItem('siteId')
      _this.wxAvatar = window.sessionStorage.getItem('wxAvatar')
      let userType = window.sessionStorage.getItem('userType')
      if (userType == 1) {
        _this.studentName = window.sessionStorage.getItem('studentName') + '（老师）'
        _this.gradeNameShow = false
        _this.personMessageShow = false
        _this.myClassShow = false
        _this.myNoticeShow = false
        _this.qjTeacherShow = true
        _this.t_show = false
        if (_this.siteId == '531B601029153CBE0129165AF7C00040') {
          _this.scanQR = true
        }
      } else {
        if (_this.siteId == '2C9146A12760F52A01276124EFB90016' || _this.siteId == '00006e72043a7f7243629eb4e1904f42c53f') {
          _this.myEwmShow = true
        }
        let params = '?orgId=' + _this.siteId + '&userId=' + _this.studentId;
        http.get(CONSTANT.STUDENTVIEW + params, {
          siteId: _this.siteId,
          studentId: _this.studentId
        }, (res) => {
          // alert(JSON.stringify(res))
          _this.content = res.data
          _this.studentName = _this.content.name
      })
        /*var params = '?siteId=' + _this.siteId + '&studentId=' + _this.studentId + '&pageNo=' + _this.pageNo;
        http.get(CONSTANT.NOTICELIST + params, {
          siteId: _this.siteId,
          studentId: _this.studentId,
          pageNo: _this.pageNo
        }, (res) => {
          console.log(res)
        _this.total = res.total
      })*/
      }
    }
  },
  methods: {
    goSomePage (witch) {
      var _this = this
      if (_this.iflogined == false) {
        _this.show = true
      } else {
        if (witch == 'per') {
          _this.$router.push({path: '/personalInformation'})
        } else if (witch == 'class') {
          _this.$router.push({path: '/myClass'})
        } else if (witch == 'notice') {
          _this.$router.push({path: '/notice'})
        } else if (witch == 'schedule') {
          _this.$router.push({path: '/schedule'})
        } else if (witch == 'ewm') {
          _this.$router.push({path: '/ewm',query:{cardNo:_this.content.cardNo}})
        } else if (witch == 'avatar') {
          _this.$router.push({path: '/avatar'})
        }
      }
    },
    showconfirm () {
      var _this = this
      _this.show = true
    },
    onCancel () {
    // 取消按钮
      var _this = this
      _this.show = false
    },
    onConfirm () {
      // 确定按钮
      var _this = this
      if (_this.tishi == '确定退出登录?') {
        http.post(CONSTANT.LOGOUT, {userId: _this.studentId,openId: _this.openId}, (res) => {
          window.sessionStorage.setItem('studentId', '-1')
          window.sessionStorage.setItem('studentName', '-1')
        })
      }
      _this.$router.push({path: '/login'})
    },
    goQuery() {
      this.$router.push({path: '/query'})
    },
    goQjTeacher() {
      this.$router.push({path: '/qj_list'})
    },
    goQdTeacher() {
      this.$router.push({path: '/qd_list_class'})
    },
    getWeixinResult() {
      let thisPageUrl = window.location.href.split('#')[0]
      http.post(CONSTANT.PAYMENT_API+"/wxConfig", {'url': thisPageUrl,'appId':this.appId}, (res) => {
        if (res.code == 200) {
          // this.str = JSON.stringify(res.obj)
          this.wx.config({
            "debug": false,
            "appId": res.obj.appid,
            "timestamp": res.obj.timestamp,
            "nonceStr": res.obj.nonceStr,
            "signature": res.obj.signature,
            "jsApiList": ['scanQRCode']
          })
          this.wx.ready(() => {
            this.wx.scanQRCode({
              needResult: 1,
              success: (res) => {
                let result = JSON.parse(res.resultStr)
                let startDate = result['startDate']
                let endDate = result['endDate']
                if (!startDate) {
                  alert('签到失败!')
                  return
                }
                if (!endDate) {
                  alert('签到失败!')
                  return
                }
                http.post(CONSTANT.QRSignIn, {
                  orgId: this.siteId,
                  humanId: this.studentId,
                  startDate: startDate,
                  endDate: endDate
                }, (_res) => {
                  alert(_res.msg)
                })
              },
              error: function(_res){
                alert('签到失败！')
              }
            })
          })
        }
      })
    }
  }
}
